<template>
<!--  <el-card style="max-width: 1000px;margin-bottom: 10px;">-->
<!--    -->
<!--  </el-card>-->
  <div class="box-container">
    <el-card style="max-width: 1000px;margin:10px 0;height: 30px;padding-left: 10px;">
      消息设置
    </el-card>

    <el-card style="max-width: 1000px;min-height: 100px;padding: 20px;box-shadow: 0 1px 5px 0 #bbbbbb">
      <div class="box">
       <div class="text">
         消息提醒 <span class="tip">（关闭后，消息将不会再提醒）</span>
       </div>
        <div class="switch-box">
          <el-radio-group v-model="settingsForm.messageRemind">
            <el-radio :value="1" size="large">开启</el-radio>
            <el-radio :value="2" size="large">关闭</el-radio>
          </el-radio-group>
        </div>
      </div>
    </el-card>

    <el-card style="max-width: 1000px;min-height: 100px;padding: 20px;margin-top: 10px;box-shadow: 0 1px 5px 0 #bbbbbb">
      <div class="box">
        <div class="text">
          消息屏蔽词 <span class="tip">（添加后，将不再接受包含屏蔽词的消息）</span>
        </div>
        <div class="add-block-word">
          <div class="init-status" v-if="hideInput" @click="hideInput = !hideInput">
            <div class="icon">+</div>
            <div class="add-text">添加屏蔽词</div>
          </div>
          <div class="input-box" v-else>
            <el-input
                v-model="blockWord"
                style="width: 240px"
                placeholder="添加关键词"
                clearable
            />
            <el-button type="primary" @click="addWord">+ 添加</el-button>
          </div>
        </div>
      </div>
    </el-card>

    <el-card style="max-width: 1000px;min-height: 100px;padding: 20px;margin-top: 15px;box-shadow: 0 1px 5px 0 #bbbbbb">
      <div class="box">
        <div class="text">
          回复我的消息提醒 <span class="tip">（接收谁的评论消息提醒）</span>
        </div>
        <el-radio-group v-model="settingsForm.acceptableReplyType">
          <el-radio :value="1" size="large">所有人</el-radio>
          <el-radio :value="2" size="large">关注的人</el-radio>
          <el-radio :value="3" size="large">不接收任何消息提醒</el-radio>
        </el-radio-group>

      </div>
      <div class="box">
        <div class="text">
          @我的消息提醒
          <span class="tip">（接收谁的@消息提醒）</span>
        </div>

        <el-radio-group v-model="settingsForm.acceptableAtType">
          <el-radio :value="1" size="large">所有人</el-radio>
          <el-radio :value="2" size="large">关注的人</el-radio>
          <el-radio :value="3" size="large">不接收任何消息提醒</el-radio>
        </el-radio-group>

      </div>
      <div class="box">
        <div class="text">
          收到的赞消息提醒
        </div>
        <div class="switch-box">
          <el-radio-group v-model="settingsForm.acceptableLikeType">
            <el-radio :value="1" size="large">开启</el-radio>
            <el-radio :value="2" size="large">关闭</el-radio>
          </el-radio-group>
        </div>
      </div>
    </el-card>


  </div>
</template>
<script setup>

import {onMounted, onUnmounted, reactive, ref, watch} from "vue";
import {getMessageSettings, updateMessageSettings} from "@/api/api.js";
import {ElMessage} from "element-plus";


const settingsForm = reactive({
  messageRemind:1,
  blockWords:'',
  acceptableReplyType:1,
  acceptableAtType:1,
  acceptableLikeType:1
})
const blockWord = ref('')

const addWord = () => {
  settingsForm.blockWords+=(","+blockWord.value)
  blockWord.value = ''
}

onMounted(async ()=>{
  const {data} = await getMessageSettings()
  settingsForm.messageRemind = data.messageRemind
  settingsForm.blockWords = data.blockWords
  settingsForm.acceptableReplyType = data.acceptableReplyType
  settingsForm.acceptableAtType = data.acceptableAtType
  settingsForm.acceptableLikeType = data.acceptableLikeType
})
const hideInput = ref(true)

const hasChanged = ref(false)
watch(settingsForm,(newValue,oldValue)=>{
  console.log('settingsForm变化了',newValue,oldValue)
  hasChanged.value = true
})
onUnmounted(async ()=>{
  console.log(settingsForm)
  try{
    if(!hasChanged.value){
      console.log("没有设置，不做操作")
      return
    }
    await updateMessageSettings(settingsForm)
    ElMessage.success("设置成功")
  }catch (e){
    ElMessage.error("错误："+e.message)
  }
})

</script>
<style lang="scss" scoped>

.box-container{
  box-sizing: border-box;
  height: 640px;
  //background-color: white;
}
.el-card{
  --el-card-padding:10px
}
:deep(.el-card__body){
  padding: 0;
}


.box{
  margin: 20px 0;
  .text{
    .tip{
      color:var(--gray-font-color--);

    }
  }
  .add-block-word{
    .init-status{
      width: 106px;
      height: 30px;
      display: flex;
      justify-content: space-between;
      align-items: center;
      .icon{
        height: 18px;
        width: 18px;
        line-height: 18px;
        text-align: center;
        border-radius: 50%;
        font-size: 13px;
        color: deepskyblue;
        background-color: #bcecff;
      }
      .add-text{
        color: deepskyblue;
      }
    }
  }

}
</style>